package com.rn.ddd.interfaces.facade;

import com.rn.ddd.application.service.MerchantService;
import com.rn.ddd.domain.merchant.entity.Merchant;
import com.rn.ddd.domain.order.event.CreateOrderEvent;
import com.rn.ddd.domain.order.entity.OrderDO;
import com.rn.ddd.infrastructure.engine.DefaultOrderEngine;
import com.rn.ddd.interfaces.assembler.MerchantAssembler;
import com.rn.ddd.interfaces.dto.MerchantDTO;
import com.rn.gypsophila.model.GlobalRet;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 为远程客户端提供粗粒度的调用接口
 *
 * @author 然诺
 */
@Api(tags = "商户接口")
@RestController
@RequestMapping("/ddd/api/merchant")
public class MerchantController {

    @Autowired
    private MerchantService merchantService;

    @ApiOperation("获取商户")
    @GetMapping("/{merchantId:\\d+}")
    public GlobalRet<MerchantDTO> getMerchant(@PathVariable Long merchantId) {
        Merchant merchant = merchantService.getMerchant(merchantId);
        return GlobalRet.success(MerchantAssembler.toDTO(merchant));
    }

    @ApiOperation("注册商户")
    @PostMapping("/registration")
    public GlobalRet<Void> registerMerchant(@Validated @RequestBody MerchantDTO dto) {
        merchantService.registerMerchant(MerchantAssembler.toEntity(dto));
        return GlobalRet.success();
    }


    @Autowired
    private DefaultOrderEngine orderEngine;

    @ApiOperation("状态机测试")
    @PostMapping("/engine")
    public GlobalRet<Void> testEngine() throws Exception {
        orderEngine.sendEvent(new CreateOrderEvent(), new OrderDO());
        return GlobalRet.success();
    }


}
